package com.greenhouse.controller;

import com.greenhouse.config.Request;
import com.greenhouse.model.Greenhouse;
import com.greenhouse.model.User;
import com.greenhouse.service.GreenhouseService;
import com.greenhouse.utily.SessionUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/greenhouse")
public class GreenhouseController {
    @Autowired
    private GreenhouseService greenhouseService;
    /**
     * 添加大棚
     */
    @RequestMapping("/add")
    public Request<Greenhouse> addGreenhouse(HttpServletRequest request,String name, String location, Integer area){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error("请先登录",null);
        }
        return greenhouseService.addGreenhouse(user.getId(),name,location,area);
    }
    /**
     * 修改大棚信息
     */
    @RequestMapping("/update")
    public Request<Greenhouse> updateGreenhouse(Greenhouse greenhouse){
        return greenhouseService.updateGreenhouse(greenhouse);
    }
    /**
     * 删除大棚
     */
    @RequestMapping("/delete")
    public Request<Boolean> deleteGreenhouse(HttpServletRequest request,Integer id){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error("请先登录",null);
        }
        greenhouseService.deleteGreenhouse(user.getId(),id);
        return Request.success("删除成功",true);
    }

    /**
     * 查看大棚列表
     * @param request
     * @return
     */
    @RequestMapping("/selectAll")
    public Request<List<Greenhouse>> selectAll(HttpServletRequest request){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error("请先登录",null);
        }
        return Request.success("查看成功",greenhouseService.selectAll(user.getId()));
    }
    /**
     * 修改大棚状态
     */
    @RequestMapping("/status")
    public Request<String> updateGreenhouseStatus(HttpServletRequest request,Integer id,Integer status){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error("请先登录",null);
        }
        return greenhouseService.updateGreenhouseStatus(id,status);
    }
}
